package com.ruoyi.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.DeliveryAddressMapper;
import com.ruoyi.product.domain.DeliveryAddress;
import com.ruoyi.product.service.IDeliveryAddressService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 地址管理Service业务层处理
 * 
 * @author guoguoa
 * @date 2025-01-07
 */
@Service
@Transactional
public class DeliveryAddressServiceImpl implements IDeliveryAddressService 
{
    @Autowired
    private DeliveryAddressMapper deliveryAddressMapper;

    /**
     * 查询地址管理
     * 
     * @param id 地址管理主键
     * @return 地址管理
     */
    @Override
    public DeliveryAddress selectDeliveryAddressById(Long id)
    {
        return deliveryAddressMapper.selectDeliveryAddressById(id);
    }

    /**
     * 查询地址管理列表
     * 
     * @param deliveryAddress 地址管理
     * @return 地址管理
     */
    @Override
    public List<DeliveryAddress> selectDeliveryAddressList(DeliveryAddress deliveryAddress)
    {
        return deliveryAddressMapper.selectDeliveryAddressList(deliveryAddress);
    }

    /**
     * 新增地址管理
     * 
     * @param deliveryAddress 地址管理
     * @return 结果
     */
    @Override
    public int insertDeliveryAddress(DeliveryAddress deliveryAddress)
    {
        // 假设 DeliveryAddress 实体类中有 userId 字段用于标识用户
        Long userId = deliveryAddress.getUserId();
        // 添加非空判断并设置默认值
        Integer active = deliveryAddress.getActive();
        if (active == null) {
            active = 1;
        }
        if (active == 0) {
            // 创建查询条件的 Map，用于查找当前用户下已有的默认地址（active 为 0）
            Map<String, Object> queryMap = new HashMap<>();
            queryMap.put("userId", userId);
            queryMap.put("active", 0);
            // 查询当前用户已有的默认地址列表
            List<DeliveryAddress> defaultAddresses = deliveryAddressMapper.selectDeliveryAddressListByUserIdAndIsDefault(queryMap);
            if (defaultAddresses!= null &&!defaultAddresses.isEmpty()) {
                // 遍历已有的默认地址列表，将它们的 active 字段修改为 1
                for (DeliveryAddress address : defaultAddresses) {
                    address.setActive(1);
                    deliveryAddressMapper.updateDeliveryAddress(address);
                }
            }
        }
        return deliveryAddressMapper.insertDeliveryAddress(deliveryAddress);
    }

    /**
     * 修改地址管理
     * 
     * @param deliveryAddress 地址管理
     * @return 结果
     */
    @Override
    public int updateDeliveryAddress(DeliveryAddress deliveryAddress)
    {
        return deliveryAddressMapper.updateDeliveryAddress(deliveryAddress);
    }

    /**
     * 批量删除地址管理
     * 
     * @param ids 需要删除的地址管理主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryAddressByIds(Long[] ids)
    {
        return deliveryAddressMapper.deleteDeliveryAddressByIds(ids);
    }

    /**
     * 删除地址管理信息
     * 
     * @param id 地址管理主键
     * @return 结果
     */
    @Override
    public int deleteDeliveryAddressById(Long id)
    {
        return deliveryAddressMapper.deleteDeliveryAddressById(id);
    }
}
